﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_Customer : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string customer_id;

        public frm_Edit_Customer()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.customer_id = null;
            LoadCountryList();
        }


        /////////////////////////
        /// Load Country List
        /////////////////////////
        private void LoadCountryList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CountryID, CountryName FROM [222_Country] ORDER BY CountryName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CountryID"].ToString();
                    item.Text = dataReader["CountryName"].ToString();
                    this.countryID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get country list");
            }
        }

        ////////////////////////////////////
        /// Load data to form
        ///////////////////////////////////        
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(customer_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_Customer] WHERE CustomerID='" + customer_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.customerID.Text = dataReader["CustomerID"].ToString();
                        this.customerName.Text = dataReader["FullName"].ToString();
                        this.addressText.Text = dataReader["Address"].ToString();
                        for (int i = 0; i < this.countryID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.countryID.Properties.Items[i];
                            if (item.Value.ToString() == dataReader["CountryID"].ToString())
                            {
                                this.countryID.SelectedIndex = i;
                            }
                        }                        
                        this.telephoneNumber.Text = dataReader["Telephone"].ToString();
                        this.faxNumber.Text = dataReader["Fax"].ToString();
                        this.picName.Text = dataReader["PIC"].ToString();
                        this.picSex.Text = dataReader["PICSex"].ToString();
                        this.picEmail.Text = dataReader["PICEmail"].ToString();
                        this.picPosition.Text = dataReader["PICPosition"].ToString();
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;

            string name = null;
            string address = null;
            string country = null;
            string telephone = null;
            string fax = null;
            string pic = null;
            string picsex = null;
            string picemail = null;
            string picposition = null;
            string taxcode = null;
            string bankaccount = null;

            ComboboxItem item = new ComboboxItem();

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.customerName.Text))
                {
                    error = "Please enter Customer Name";
                }
                else
                {
                    name = this.customerName.Text;
                }
            }            

            if (string.IsNullOrEmpty(error))
            {
                address = this.addressText.Text;
                if (this.countryID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.countryID.SelectedItem;
                    country = item.Value.ToString();
                }
                telephone = this.telephoneNumber.Text;
                fax = this.faxNumber.Text;
                pic = this.picName.Text;
                picsex = this.picSex.Text;
                picemail = this.picEmail.Text;
                picposition = this.picPosition.Text;
                taxcode = this.taxCode.Text;
                bankaccount = this.bankAccount.Text;

                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_Customer] SET FullName='" + name + "', Address='" + address + "',"
                        + " CountryID=" + country + ", Telephone='" + telephone + "', Fax='" + fax + "', PIC='" + pic + "',"
                        + " PICSex='" + picsex + "', PICEmail='" + picemail + "', PICPosition='" + picposition + "',"
                        + " TaxCode='" + taxcode + "', BankAccount='" + bankaccount + "'"
                        + " WHERE CustomerID='" + customer_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Customer updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Customer. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
